package org.jeecg.modules.demo.sermanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.jeecg.modules.demo.sermanager.entity.NetptJob;
import org.jeecg.modules.demo.sermanager.entity.NetptJobSer;
import org.jeecg.modules.demo.sermanager.entity.NetptJobDetection;
import org.jeecg.modules.demo.sermanager.entity.NetptJobIplist;
import org.jeecg.modules.demo.sermanager.entity.NetptJobRet;
import org.jeecg.modules.demo.sermanager.mapper.NetptJobSerMapper;
import org.jeecg.modules.demo.sermanager.mapper.NetptJobDetectionMapper;
import org.jeecg.modules.demo.sermanager.mapper.NetptJobIplistMapper;
import org.jeecg.modules.demo.sermanager.mapper.NetptJobRetMapper;
import org.jeecg.modules.demo.sermanager.mapper.NetptJobMapper;
import org.jeecg.modules.demo.sermanager.service.INetptJobService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: netpt_job
 * @Author: jeecg-boot
 * @Date:   2022-01-07
 * @Version: V1.0
 */
@Service
public class NetptJobServiceImpl extends ServiceImpl<NetptJobMapper, NetptJob> implements INetptJobService {

	@Autowired
	private NetptJobMapper netptJobMapper;
	@Autowired
	private NetptJobSerMapper netptJobSerMapper;
	@Autowired
	private NetptJobDetectionMapper netptJobDetectionMapper;
	@Autowired
	private NetptJobIplistMapper netptJobIplistMapper;
	@Autowired
	private NetptJobRetMapper netptJobRetMapper;


	@Override
	@Transactional(rollbackFor=Exception.class)
	public void saveMain(NetptJob netptJob, NetptJobSer netptJobSer, List<NetptJobDetection> netptJobDetectionList) {
		netptJobMapper.insert(netptJob);

		if(netptJobDetectionList!=null && netptJobDetectionList.size()>0) {
			for(NetptJobDetection entity:netptJobDetectionList) {
				NetptJobDetection nt=new NetptJobDetection();
				BeanUtils.copyProperties(entity,nt);
				//外键设置
				nt.setJobId(netptJob.getId());
				netptJobDetectionMapper.insert(nt);
			}
		}
		//有检测目标，nmap扫描
		if(netptJob.getDetectionTarget().equals("1")){
			for(int i=0;i<netptJob.getTaskTarget().split(",").length;i++){
				NetptJobIplist entity=new NetptJobIplist();
				entity.setJobId(netptJob.getId());
				entity.setIp(netptJob.getTaskTarget().split(",")[i]);
			}
		}else{
			for(int i=0;i<netptJob.getTaskTarget().split(",").length;i++){
				NetptJobSer ns=new NetptJobSer();
				BeanUtils.copyProperties(netptJobSer,ns);

				//外键设置
				ns.setJobId(netptJob.getId());
				ns.setIp(netptJob.getTaskTarget().split(",")[i]);
				netptJobSerMapper.insert(ns);
			}
		}

	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void updateMain(NetptJob netptJob,NetptJobSer netptJobSer,List<NetptJobDetection> netptJobDetectionList) {
		netptJobMapper.updateById(netptJob);
		//1.先删除子表数据
		netptJobSerMapper.deleteByMainId(netptJob.getId());
		netptJobDetectionMapper.deleteByMainId(netptJob.getId());
		netptJobIplistMapper.deleteByMainId(netptJob.getId());
		netptJobRetMapper.deleteByMainId(netptJob.getId());
		int len=netptJob.getTaskTarget().split(",").length;
		String obj []=netptJob.getTaskTarget().split(",");
		for(int i=0;i<len;i++){
			//外键设置
			netptJobSer.setJobId(netptJob.getId());
			netptJobSer.setIp(obj[i]);
			netptJobSerMapper.insert(netptJobSer);
		}
		if(netptJobDetectionList!=null && netptJobDetectionList.size()>0) {
			for(NetptJobDetection entity:netptJobDetectionList) {
				//外键设置
				entity.setJobId(netptJob.getId());
				netptJobDetectionMapper.insert(entity);
			}
		}
		//有检测目标，nmap扫描
		if(netptJob.getDetectionTarget().equals("1")){
			for(int i=0;i<len;i++){
				NetptJobIplist entity=new NetptJobIplist();
				entity.setJobId(netptJob.getId());
				entity.setIp(obj[i]);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		netptJobSerMapper.deleteByMainId(id);
		netptJobDetectionMapper.deleteByMainId(id);
		netptJobIplistMapper.deleteByMainId(id);
		netptJobRetMapper.deleteByMainId(id);
		netptJobMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			netptJobSerMapper.deleteByMainId(id.toString());
			netptJobDetectionMapper.deleteByMainId(id.toString());
			netptJobIplistMapper.deleteByMainId(id.toString());
			netptJobRetMapper.deleteByMainId(id.toString());
			netptJobMapper.deleteById(id);
		}
	}

	@Override
	public IPage<NetptJob> findNetptJobList(IPage<NetptJob> page, QueryWrapper<NetptJob> queryWrapper) {
		return netptJobMapper.findNetptJobList(page,queryWrapper);
	}

}
